package com.skwen.delivery.superdelivery.login.loginImpInterface;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.skwen.delivery.superdelivery.http.AppUrl;
import com.skwen.delivery.superdelivery.http.Const;
import com.skwen.delivery.superdelivery.login.bean.MyResponseEntity;
import com.skwen.delivery.superdelivery.login.bean.User;
import com.skwen.delivery.superdelivery.login.loginInterface.LoginListener;
import com.skwen.delivery.superdelivery.login.loginInterface.UserBis;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yundantian on 2017/8/28.
 *
 */

public class UserBisImp implements UserBis {


    @Override
    public void login(String nickName, String password, LoginListener loginListener) {


        Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

        AppUrl appUrl = retrofit.create(AppUrl.class);

        Call<MyResponseEntity> call = appUrl.login(nickName, password);

        call.enqueue(new Callback<MyResponseEntity>() {
            @Override
            public void onResponse(Call<MyResponseEntity> call, Response<MyResponseEntity> response) {
                try {
                    MyResponseEntity map = response.body();
                    if (map.getCode() == 200) {
                        Gson gson = new GsonBuilder().create();
                        User user = gson.fromJson(map.getBody().toString(), User.class);
                        loginListener.loginSucess(user);
                    } else {
                        loginListener.loginFaile(map.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<MyResponseEntity> call, Throwable throwable) {
                if ("root".equals(nickName) && "1234".equals(password)) {
                    User user = new User();
                    user.setUserName(nickName);
                    user.setUserPsd(password);
                    loginListener.loginSucess(user);
                } else {
                    loginListener.loginFaile("登录失败");
                }
            }

        });
    }

    @Override
    public void forgetPassword(String nickName) {

    }

    @Override
    public void register(User user, LoginListener loginListener) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

        AppUrl appUrl = retrofit.create(AppUrl.class);

        Call<MyResponseEntity> call = appUrl.register(user);

        call.enqueue(new Callback<MyResponseEntity>() {
            @Override
            public void onResponse(Call<MyResponseEntity> call, Response<MyResponseEntity> response) {

                MyResponseEntity map = response.body();
                try {
                    if (map.getCode() == 200) {
                        Gson gson = new GsonBuilder().create();
                        User user = gson.fromJson(map.getBody().toString(), User.class);
                        loginListener.loginSucess(user);
                    } else {
                        loginListener.loginFaile("注册失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<MyResponseEntity> call, Throwable t) {
                loginListener.loginFaile("注册失败");
            }
        });
    }
}
